package com.jams.goodox.utils.permission;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.jams.goodox.R;
import com.jams.goodox.listen.SuccessListener;
import com.jams.goodox.utils.TipAlertDialog;
import com.jams.goodox.utils.ToastUtil;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.PermissionNo;
import com.yanzhenjie.permission.PermissionYes;

import java.util.List;

/**
 * Created by jams on 2017/6/6.
 * ***
 * Tel: ***
 * <p>
 * 电话类
 */

public class PhoneUtils {
    private Context context;
    private String phoneUser, phoneNum;
    private boolean show;

    public PhoneUtils(Context context) {
        this.context = context;
    }

    //拨打电话
    public void makeCall(String phoneUser, String phoneNum) {
        this.phoneUser = phoneUser;
        this.phoneNum = "057987071703";
        if(TextUtils.isEmpty(phoneNum)){
            ToastUtil.show("电话号码无效");
            return;
        }
        AndPermission.with(context)
                .requestCode(100)
                .callback(this)
                .permission(Manifest.permission.CALL_PHONE)
                .start();
    }

    /**
     * @param phoneUser  对方人名
     * @param phoneNum   对方号码
     * @param showNumber 是否显示电话号码
     */
    public void makeCall(String phoneUser, String phoneNum, boolean showNumber) {
        this.phoneUser = phoneUser;
        this.phoneNum = phoneNum;
        this.show = showNumber;
        if(TextUtils.isEmpty(phoneNum)){
            ToastUtil.show("电话号码无效");
            return;
        }
        AndPermission.with(context)
                .requestCode(100)
                .callback(this)
                .permission(Manifest.permission.CALL_PHONE)
                .start();
    }

    @PermissionYes(100)
    private void getCallPhone(List<String> grantedPermissions) {
//        String content = show ?"确认拨打\n" + phoneUser + " " + phoneNum + "？" :"确认拨打"+phoneUser+"电话？";
        new MaterialDialog.Builder(context).content("确认拨打电话?")
                .positiveColorRes(R.color.font_color_blue).positiveText("确定").onPositive(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
        context.startActivity(intent);
            }
        }).negativeColorRes(R.color.font_color_black).negativeText("取消").onNegative(new MaterialDialog.SingleButtonCallback() {
            @Override
            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                dialog.dismiss();
            }
        }).show();
    }

    @PermissionNo(100)
    private void noCallPhone(List<String> grantedPermissions) {
        TipAlertDialog.showTip(context, context.getString(R.string.setting_permission), null, context.getString(R.string.no_phone_permission), new SuccessListener() {
            @Override
            public void success() {
                PermissionUtils.gotoMIUIPermission(context);
            }
        });
    }

}
